$(function() {
  var pageSize = 5;
  var currentPage = 1;

  function render() {
    $.ajax({
      type: "get",
      url: "/user/queryUser",
      data: {
        pageSize: pageSize,
        page: currentPage
      },
      dataType: "json",
      success: function(res) {
        console.log(res);
        $("tbody").html(template("tmp", res));
        setPage(res.total);
      }
    });
  }
  render();

  //2.禁用启用功能
  var id = null;
  var isDelete = null;
  $('tbody').on('click','.my-btn',function(){

    id = $(this).parent().data('id');
    isDelete = $(this).hasClass('btn-danger') ? 0:1;
    console.log(id,isDelete);
    
  });
  $('.btn-ok').click(function(){
    $.ajax({
      type: "post",
      url: "/user/updateUser",
      data: {
        id: id,
        isDelete: isDelete
      },
      dataType: "json",
      success: function (res) {
        console.log(res);
        //重新渲染
        render();
        //隐藏模态框
        $('.modal-user').modal('hide');
        
      }
    });
  })
  //分页功能
  function setPage(total){
    $("#paginator").bootstrapPaginator({
      bootstrapMajorVersion:3,//默认是2，如果是bootstrap3版本，这个参数必填
      currentPage:currentPage,//当前页
      totalPages:Math.ceil(total / pageSize),//总页数
      size:"small",//设置控件的大小，mini, small, normal,large
      onPageClicked:function(event, originalEvent, type,page){
        //为按钮绑定点击事件 page:当前点击的按钮值
        console.log(page);
        currentPage = page;
        render();
        
      }
  });
  }


});
